use cortex_m_rt::exception;

use crate::c::{
    osDelay, osDelayUntil, osKernelGetTickCount, osKernelInitialize, osKernelStart, PendSV_Handler,
    SVC_Handler, SysTick_Handler,
};

pub mod event_flags;
pub mod message_queue;
pub mod mutex;
pub mod thread;

#[inline(always)]
pub fn delay(ms: u32) {
    unsafe {
        osDelay(ms);
    }
}

#[inline(always)]
pub fn delay_until(tick: u32) {
    unsafe {
        osDelayUntil(tick);
    }
}

#[inline(always)]
pub fn kernel_get_tick_count() -> u32 {
    unsafe { osKernelGetTickCount() }
}

pub fn system_init() {

    extern "C" {
        fn SystemInit();
    }

    unsafe {
        SystemInit();
    }
}

#[inline(always)]
pub fn kernel_initialize() {
    unsafe { osKernelInitialize() };
}

#[inline(always)]
pub fn kernel_start() {
    unsafe { osKernelStart() };
}

#[exception]
fn SysTick() {
    unsafe {
        SysTick_Handler();
    }
}

#[exception]
fn PendSV() {
    unsafe {
        PendSV_Handler();
    }
}

#[exception]
fn SVCall() {
    unsafe {
        SVC_Handler();
    }
}
